﻿/**************************************************************
 *
 * 唯一标识：698d5fc9-560a-4751-8142-29c528a8e7fb
 * 命名空间：Sgr.UPMS.Application.Validations.Departments
 * 创建时间：2023/8/27 20:38:42
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：
 *
 **************************************************************/

using FluentValidation;
using Sgr.UPMS.Application.Commands.Departments;
using Sgr.Utilities;

namespace Sgr.UPMS.Application.Validations.Departments
{
    /// <summary>
    /// 更新部门命令验证器
    /// </summary>
    public class UpdateDepartmentCommandValidator : AbstractValidator<UpdateDepartmentCommand>
    {
        public UpdateDepartmentCommandValidator()
        {
            RuleFor(command => command.DepartmentId)
                .GreaterThan(0)
                .WithMessage("部门标识必须大于0");

            RuleFor(command => command.Name)
                .NotEmpty()
                .WithMessage("部门名称不能为空")
                .MaximumLength(100)
                .WithMessage("部门名称不能超过100个字符");

            RuleFor(command => command.Remarks)
                .MaximumLength(200)
                .WithMessage("备注不能超过200个字符");

            RuleFor(command => command.State)
                .IsInEnum()
                .WithMessage("职务状态只能为Normal或Deactivate");

            RuleFor(command => command.Leader)
                .MaximumLength(20)
                .WithMessage("负责人名称不能超过20个字符");

            RuleFor(command => command.Phone)
                .MaximumLength(20)
                .WithMessage("联系电话不能超过20个字符");

            RuleFor(command => command.Email)
                .MaximumLength(30)
                .WithMessage("联系邮箱不能超过30个字符");


            // 手机号码格式验证
            When(command => !string.IsNullOrEmpty(command.Phone), () =>
            {
                RuleFor(command => command.Phone)
                    .Must(phone => phone != null && RegexHelper.IsValidPhoneNumber(phone))
                    .WithMessage("请输入有效的联系电话");
            });

            // 电子邮箱格式验证
            When(command => !string.IsNullOrEmpty(command.Email), () =>
            {
                RuleFor(command => command.Email)
                    .Must(email => email != null && RegexHelper.IsValidEmail(email))
                    .WithMessage("请输入有效的联系邮箱");
            });
        }
    }
}